<template>
  <div class="category-container">
    <el-card class="box-card">
      <!-- 搜索表单 -->
      <el-form :model="queryParams" ref="queryRef" :inline="true">
        <el-form-item label="类别名称" prop="categoryName">
          <el-input
            v-model="queryParams.categoryName"
            placeholder="请输入类别名称"
            clearable
            style="width: 200px"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="queryParams.status" placeholder="类别状态" clearable style="width: 200px">
            <el-option
              v-for="dict in sys_normal_disable"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 操作按钮 -->
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" plain icon="Plus" @click="handleAdd">新增</el-button>
        </el-col>
      </el-row>

      <!-- 数据表格 -->
      <el-table v-loading="loading" :data="categoryList">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="类别编号" align="center" prop="categoryId" />
        <el-table-column label="类别名称" align="center" prop="categoryName" />
        <el-table-column label="显示顺序" align="center" prop="orderNum" />
        <el-table-column label="状态" align="center" prop="status">
          <template #default="scope">
            <dict-tag :options="sys_normal_disable" :value="scope.row.status"/>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center" prop="createTime" width="180" />
        <el-table-column label="操作" align="center" width="200" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button link type="primary" icon="Edit" @click="handleEdit(scope.row)">修改</el-button>
            <el-button link type="danger" icon="Delete" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页组件 -->
      <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { listCategory, getCategory, addCategory, updateCategory, delCategory } from "@/api/ich/category"
import { useDict } from '@/utils/dict'
import Pagination from '@/components/Pagination'
import DictTag from '@/components/DictTag'

// 加载字典数据
const { sys_normal_disable } = await useDict('sys_normal_disable')

const loading = ref(false)
const total = ref(0)
const categoryList = ref([])
const queryRef = ref()

// 查询参数
const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  categoryName: undefined,
  status: undefined
})

/** 查询列表 */
function getList() {
  loading.value = true
  listCategory(queryParams.value).then(response => {
    categoryList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  queryRef.value?.resetFields()
  handleQuery()
}

/** 新增按钮操作 */
function handleAdd() {
  // TODO: 实现新增逻辑
}

/** 修改按钮操作 */
function handleEdit(row) {
  // TODO: 实现修改逻辑
}

/** 删除按钮操作 */
function handleDelete(row) {
  // TODO: 实现删除逻辑
}

onMounted(() => {
  getList()
})
</script>

<style lang="scss" scoped>
.category-container {
  padding: 20px;
}
.mb8 {
  margin-bottom: 8px;
}
</style> 